﻿using System;
using System.Windows.Forms;
using TestesFixacao.WindowsApp.Refatorado.Controls.Shared;
using TestesFixacao.WindowsApp.Refatorado.Extensions;

namespace TestesFixacao.WindowsApp.Refatorado
{
    public partial class Main : Form
    {
        private static Main _instance;
        private IDataManager _dataManager;
        private UserControl control;

        public Main()
        {
            InitializeComponent();
            _instance = this;
        }

        public static Main Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Main();
                }
                return _instance;
            }
        }

        public void ShowMessageInFooter(string message)
        {
            lblStatus.Text = message;
        }

        private void LoadDataManager(DataManagerEnum key)
        {
            try
            {
                IDataManager newManager = DataManagerFactory.GetManager(key);

                if (newManager != null && _dataManager != newManager)
                {
                    if (control != null)
                    {
                        //control.Dispose();
                        panControl.Controls.Clear();
                    }

                    _dataManager = newManager;
                    control = _dataManager.GetControl();

                    control.Width = panControl.Width;
                    control.Height = panControl.Height;
                    control.Dock = DockStyle.Fill;
                    panControl.Controls.Add(control);
                    toolbar.Items["description"].Text = _dataManager.Description();

                    ToolTipMessage toolTip = _dataManager.GetToolTipMessage();
                    if (toolTip != null)
                    {
                        btnAdd.ToolTipText = toolTip.Add;
                        btnDelete.ToolTipText = toolTip.Delete;
                        btnSet.ToolTipText = toolTip.Edit;
                    }

                    ActionButtonEnabled actions = _dataManager.GetActionButtonEnabled();
                    if (actions != null)
                    {
                        btnAdd.Enabled = actions.Add;
                        btnDelete.Enabled = actions.Remove;
                        btnSet.Enabled = actions.Edit;
                    }
                }
                toolbar.Enabled = _dataManager != null;
            }
            catch (Exception be)
            {
                MessageBoxExtension.ShowError(be.Message + be.InnerException);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                _dataManager.AddData();
            }
            catch (Exception exc)
            {
                MessageBoxExtension.ShowError(exc.Message + exc.InnerException);
            }
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            try
            {
                _dataManager.EditData();
            }
            catch (Exception exc)
            {
                MessageBoxExtension.ShowError(exc.Message + exc.InnerException);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                _dataManager.RemoveData();
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message + exc.InnerException);
            }
        }

        private void matériasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDataManager(DataManagerEnum.Materias);
        }

        private void disciplinasToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDataManager(DataManagerEnum.Disciplinas);
        }

        private void SerieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDataManager(DataManagerEnum.Serie);
        }

        private void QuestoesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDataManager(DataManagerEnum.Questoes);
        }

        private void testeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadDataManager(DataManagerEnum.Teste);
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}